package com.gdcp.demo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

import javax.net.ssl.SSLContext;

public class MainActivity extends AppCompatActivity {
    private static final int GET_RESULT = 1001;
    final String TAG = "MainActivity";
    EditText editText;
    Button button1;
    Handler handler ;
    TextView resultText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Http");
        resultText =  findViewById(R.id.result_text);
        setContentView(R.layout.activity_main);
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == GET_RESULT){
                    Log.e("aaaaaaaaaaaaaa",(String) msg.obj);
                    String html = (String) msg.obj;

                    resultText.setText(html);
                    return true;
                }
                return false;
            }
        });
        editText =findViewById(R.id.edit_txt);

        button1 = findViewById(R.id.http_get_btn1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String phone = editText.getText().toString();
                //new 一个人线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        useHttpGet(phone);
                    }
                }).start();
            }

            private void useHttpGet(String phone) {
                String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=";
                url = url.concat(phone).concat("&userID=");
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);

                try {
                    HttpResponse httpResponse =  httpClient.execute(httpGet);
                    //获取请求码
                    int code = httpResponse.getStatusLine().getStatusCode();
                    Log.e(TAG,"code = "+code);
                    //获取请求实体  (String 类型)
                    String htmlContent = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
                    Log.e(TAG,htmlContent);
                    //获得一个Message
                    Message message=Message.obtain();
                    message.what = GET_RESULT;
                    message.obj = htmlContent   ;
                    Log.e(TAG,message.obj.toString())     ;
                    handler.sendMessage(message);
                    
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
